﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace cancercurer
{
    /// <summary>
    /// Logique d'interaction pour MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Reseau reseau;
        private Random rand = new Random();

        List<People> baseOfLearning = new List<People>();
        List<People> baseOfValidation = new List<People>();

        public MainWindow()
        {
            InitializeComponent();
            reseau = new Reseau(4);
            reseau.Initialize();
            readAndSerialize();
        }

        /// <summary>
        /// Read the text file and searealize into a table of people
        /// </summary>
        public void readAndSerialize()
        {
            try
            {
                int counter = 0;
                string line;

                // Read the file and serialize
                System.IO.StreamReader file =
                   new System.IO.StreamReader(@"res_neuron_apprentissage.txt");
                while ((line = file.ReadLine()) != null)
                {
                    string[] data = line.Split(',');
                    if (counter <= 231)
                    {
                        baseOfLearning.Add(new People(int.Parse(data[0]), int.Parse(data[1]), int.Parse(data[2]), int.Parse(data[3]) - 1));
                    }
                    else if (counter > 231)
                    {
                        baseOfValidation.Add(new People(int.Parse(data[0]), int.Parse(data[1]), int.Parse(data[2]), int.Parse(data[3]) - 1));
                    }
                    counter++;
                }
                file.Close();

                shuffle(baseOfLearning);
                shuffle(baseOfValidation);
            }
            catch (Exception e)
            {
                MessageBox.Show("Erreur :" + e.Message);
            }
        }

        private void btLearning_Click(object sender, RoutedEventArgs e)
        {
            int nombreTours = 0;
            double result = 100.0;
            double precResult = 101.00;
            int sortie = 0;

            while (sortie < 2 && nombreTours < 300)
            {
                for (int i = 0; i < baseOfLearning.Count(); i++)
                {
                    reseau.Learning(baseOfLearning[i]);
                }

                result = reseau.GetTotalError();
                Console.Write("Erreur totale Appr : " + result);

                Reseau.tauxApprentissage = Reseau.tauxApprentissage * 0.995f;

                for (int i = 0; i < baseOfValidation.Count(); i++)
                {
                    reseau.evaluate(baseOfValidation[i]);
                }

                precResult = result;
                result = reseau.GetTotalError();

                if (result > precResult)
                    sortie++;

                Console.WriteLine(" --- Erreur totale Valid : " + result + " taux d'apprentissage =  " + Reseau.tauxApprentissage);
                Console.WriteLine(reseau.GetResultRate());

                nombreTours++;
            }
        }

        private void btTesting_Click(object sender, RoutedEventArgs e)
        {
            int result;
            int age = int.Parse(inputAge.Text);
            int year = int.Parse(inputOperation.Text);
            int nbr = int.Parse(inputNodes.Text);
            result = this.evaluate(age, year, nbr);

            if (result == 0)
            {
                //MessageBox.Show("Vous allez vivre plus de 5 ans");
                MessageBox.Show((result + 1).ToString());
            }
            else
            {
                //MessageBox.Show("Vous allez vivre moins de 5 ans");
                MessageBox.Show((result + 1).ToString());
            }
        }

		private int evaluate(int _age , int _year , int _nbr)
        {
            int result;

            result = reseau.evaluate(_age, _year, _nbr);

            return result;
        }

        private void shuffle(List<People> list)
        {
            Random rand = new Random();
            int n = list.Count;
            while (n > 1)
            {
                n--;
                int k = rand.Next(n + 1);
                People value = list[k];
                list[k] = list[n];
                list[n] = value;
            } 
        }
    }
}
